#include "LevelManager.h"
#include "GameManager.h"

using namespace Ogre;

static const int TIME_PER_LEVEL = 30;

template<> LevelManager* Ogre::Singleton<LevelManager>::ms_Singleton = 0;

LevelManager::LevelManager()
:mTimeUntilNextEnemy(-1),
mTimeUntilNextFish(0),
mTimeUntilNextCustomObject(0),
mTimeUntilNextBonus(25.f),
mTimeUntilNextWeapon(0),
mTimeTotal(1),
pause(0),
level(0),
difficulty(0),
lastLevel(false),
mTimeUntilNextLevel(TIME_PER_LEVEL),
currentWeaponType(Weapon::SNOW),
currentWeaponLevel(Weapon::LEVEL1),
tirage(0)

{}

bool LevelManager::frameStarted(const FrameEvent& evt)
{
	if(!pause){
		mTimeTotal += evt.timeSinceLastFrame;
		mTimeLevel += evt.timeSinceLastFrame;
		difficulty = mTimeLevel * level;


		difficulty = mTimeTotal * level;

		// Creation des objets de la scene
		if (mTimeUntilNextEnemy >= 0)
			mTimeUntilNextEnemy -= evt.timeSinceLastFrame;
		else
		{
			
			if (!lastLevel)
				tirage = (int) Ogre::Math::RangeRandom(1.f, (difficulty/(TIME_PER_LEVEL))+1);
			else
				tirage = (int) Ogre::Math::RangeRandom(4.f, 8.f);


			std::cout << difficulty << " = " <<mTimeTotal << " * " << level<< std::endl;
			std::cout << "tirage = " <<tirage<< std::endl;

			switch(tirage){
			case 1 : 
				GameManager::getSingleton().createEnemy(Enemy::WIN311);
				break;
			case 2 : 
				GameManager::getSingleton().createEnemy(Enemy::WIN95);
				break;
			case 3 : 
				GameManager::getSingleton().createEnemy(Enemy::WIN98);
				break;
			case 4 : 
				GameManager::getSingleton().createEnemy(Enemy::WINME);
				break;
			case 5 : 
				GameManager::getSingleton().createEnemy(Enemy::WIN2000);
				break;
			case 6:
				GameManager::getSingleton().createEnemy(Enemy::HOLE);
				break;
			case 7 : 
				GameManager::getSingleton().createEnemy(Enemy::XP);
				break;
			case 8 : 
				GameManager::getSingleton().createEnemy(Enemy::APPLE);
				break;
			default : 
				if ( tirage > 11){
					if (!lastLevel)
						GameManager::getSingleton().createEnemy(Enemy::VISTA);
					lastLevel = true;
				}
				//GameManager::getSingleton().createEnemy(Enemy::APPLE);
				//GameManager::getSingleton().createEnemy(Enemy::XP);
			}

			//if(!lastLevel)
				mTimeUntilNextEnemy = Ogre::Math::RangeRandom(1.f, 2.f);
			//else
			//	mTimeUntilNextEnemy = 1.f;
			//Ogre::Math::RangeRandom(1.f, TIME_PER_LEVEL/(((int)mTimeTotal)%TIME_PER_LEVEL));
		}

		if (mTimeUntilNextFish >= 0)
			mTimeUntilNextFish -= evt.timeSinceLastFrame;
		else
		{
			GameManager::getSingleton().createFish();
			mTimeUntilNextFish = Ogre::Math::RangeRandom(1.f, 5.f);	
		}


		if (mTimeUntilNextLevel>= 0)
			mTimeUntilNextLevel -= evt.timeSinceLastFrame;
		else
		{
			level++;
			mTimeLevel = 0;
			std::cout << "level = " <<level<< std::endl;
			GameManager::getSingleton().createBonus(Bonus::LIFE);
			mTimeUntilNextLevel = TIME_PER_LEVEL;
			
		}


		if (mTimeUntilNextCustomObject >= 0)
			mTimeUntilNextCustomObject -= evt.timeSinceLastFrame;
		else
		{
			GameManager::getSingleton().createCustomObject();
			mTimeUntilNextCustomObject = Ogre::Math::RangeRandom(1.f, 3.f);	
		}


		if (mTimeUntilNextBonus >= 0)
			mTimeUntilNextBonus -= evt.timeSinceLastFrame;
		else
		{
			GameManager::getSingleton().createBonus(Bonus::SHIELD);
			mTimeUntilNextBonus = 25.f;//Ogre::Math::RangeRandom(1.f, 3.f);	
		}



		if (mTimeUntilNextWeapon >= 0)
			mTimeUntilNextWeapon -= evt.timeSinceLastFrame;
	}
	return true;
}

void LevelManager::callForNewWeapon(const Ogre::Vector3& position)
{
	if (mTimeUntilNextWeapon < 0)
	{
		Weapon* weapon = GameManager::getSingleton().createWeapon(currentWeaponType, currentWeaponLevel);
		weapon->getSceneNode()->setPosition( position );
		mTimeUntilNextWeapon = Ogre::Math::RangeRandom(3.0f, 15.0f);
	}
}

void LevelManager::resume(void){
	pause = !pause;
}


void LevelManager::toggleWeapon()
{
	switch(currentWeaponType)
	{
	case Weapon::SNOW:
		switch(currentWeaponLevel)
		{
		case Weapon::LEVEL1:
			currentWeaponLevel = Weapon::LEVEL2;
			break;
		case Weapon::LEVEL2:
			currentWeaponType = Weapon::ICE;
			currentWeaponLevel = Weapon::LEVEL1;
			break;
		}
		break;
	case Weapon::ICE:
		switch(currentWeaponLevel)
		{
		case Weapon::LEVEL1:
			currentWeaponLevel = Weapon::LEVEL2;
			break;
		case Weapon::LEVEL2:
			currentWeaponType = Weapon::WATER;
			currentWeaponLevel = Weapon::LEVEL1;
			break;
		}
		break;
	case Weapon::WATER:
		switch(currentWeaponLevel)
		{
		case Weapon::LEVEL1:
			currentWeaponLevel = Weapon::LEVEL2;
			break;
		case Weapon::LEVEL2:
			currentWeaponType = Weapon::SNOW;
			currentWeaponLevel = Weapon::LEVEL1;
			break;
		}
		break;
	}

}

int LevelManager::getLevel(){
	return level;

}